package org.example.param;

import lombok.extern.log4j.Log4j2;

import java.io.*;
import java.util.Properties;
@Log4j2
public class Config {
    private static Properties config;

    public static Properties getConfig(){
        if (config == null){
            config = new Properties();
            try (InputStream resource = Config.class.getClassLoader().getResourceAsStream("config.properties")) {
                config.load(resource);
                log.info("配置加载成功");
            } catch (IOException e) {
                log.info("配置加载失败");
                throw new RuntimeException(e);
            }
        }
        return config;
    }


    public static void update(String key,String newValue){
        if (key == null && key.isEmpty()){
            return;
        }
        config.setProperty(key,newValue);
        //更新配置文件
        log.info("更新配置文件");

        try(OutputStream outputStream = new FileOutputStream(Config.class.getClassLoader().getResource("config.properties").getFile())) {
            config.store(outputStream,null);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }




}
